package com.buli.blmall.admin.system.service;

import com.buli.blmall.admin.system.entity.SysUser;
import com.buli.blmall.admin.system.vo.SysUserVO;
import com.buli.blmall.admin.system.mapper.SysUserMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import cn.hutool.core.bean.BeanUtil;
import java.util.List;

/**
 * <p>
 * 用户 服务类
 * </p>
 *
 * @author buli
 * @since 2024-12-20
 */
@Service
public class SysUserService extends ServiceImpl<SysUserMapper, SysUser> {

    /**
     * 查询用户详情
     *
     * @param id
     * @return 用户详情
     */
    public SysUserVO info(Long id) {
        return BeanUtil.copyProperties(getById(id), SysUserVO.class );
    }

    /**
     * 查询用户集合
     *
     * @param sysUserVo
     * @return 用户集合
     */
    public List<SysUserVO> queryList(SysUserVO sysUserVo) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        List<SysUser> list = list(queryWrapper);
        return BeanUtil.copyToList(list, SysUserVO.class );
    }

    /**
     * 新增用户
     * @param sysUserVo
     * @return boolean
     */
    public boolean add(SysUserVO sysUserVo) {
        SysUser sysUser = BeanUtil.copyProperties(sysUserVo, SysUser.class );
        return save(sysUser);
    }

    /**
     * 编辑用户
     * @param sysUserVo
     * @return boolean
     */
    public boolean edit(SysUserVO sysUserVo) {
        SysUser sysUser = BeanUtil.copyProperties(sysUserVo, SysUser.class );
        return updateById(sysUser);
    }

    /**
     * 删除用户
     * @param id
     * @return boolean
     */
    public boolean delete(Long id) {
        return removeById(id);
    }

    /**
     * 根据用户名查询用户
     * @param username
     * @return
     */
    public SysUser getByUsername(String username) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getUsername, username);
        return getOne(queryWrapper);
    }

    /**
     * 根据手机号查询用户
     * @param mobile
     * @return
     */
    public SysUser getByMobile(String mobile) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getMobile, mobile);
        return getOne(queryWrapper);
    }

}
